<?php
import( 'de.ceus-media.framework.neon.DefinitionAction' );
import( 'classes.models.Role' );
import( 'classes.models.UserRole' );
/**
 *	Actions for Users.
 *	@package		tools.BugTracker
 *	@extends		CMF_Krypton_DefinitionAction
 *	@uses			Role
 *	@uses			UserRole
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@since			01.11.2005
 *	@version		0.6
 */
/**
 *	Actions for Users.
 *	@package		tools.BugTracker
 *	@extends		CMF_Krypton_DefinitionAction
 *	@uses			Role
 *	@uses			UserRole
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@since			01.11.2005
 *	@version		0.6
 *	@todo			Right Check Integration
 */
class UserActions extends CMF_Krypton_DefinitionAction
{
	/**
	 *	Executes called Actions.
	 *	@access		public
	 *	@return		void
	 */
	public function act()
	{
		$config		= $this->ref->get( 'config' );
		$request	= $this->ref->get( 'request' );
		$auth		= $this->ref->get( 'auth' );

		$this->loadLanguage( 'user' );

		if( !$auth->isAuthenticated() )
			$this->restart( "./" );

		$user_id	= $request->get( 'user_id' );
		if( $user_id )
		{
			if( $request->get( 'removeUser' ) )
				$this->removeUser();
			else if( $request->get( 'editUser' ) )
				$this->editUser();
			else if( $request->get( 'addRole' ) )
				$this->addRole();
			else if( $request->get( 'removeRole' ) )
				$this->removeRole();
		}
		else
		{
			$this->filterUser();
			if( $request->get( 'addUser' ) )
				$this->addUser();
		}
	}

	/**
	 *	Adds a new User.
	 *	@access		private
	 *	@return		void
	 */
	private function addUser()
	{
		$request		=& $this->ref->get( 'request' );
		if( $this->validateForm( 'user', 'addUser', 'user', 'add' ) )
		{
			$username	= $request->get( 'add_username' );
			$email		= $request->get( 'add_email' );
			$password	= $request->get( 'add_password' );
			$confirm		= $request->get( 'add_confirm' );
			if( $role_id = $request->get( 'add_role' ) )
			{
				$user	= new User();
				$users	= $user->getAllData();
				$users	= $users[$user->getTableName()];
				foreach( $users as $user )
				{
					if( strtolower( $user['username'] ) == strtolower( $username ) )
						return $this->messenger->noteError( $this->words['user']['msg']['error3'], $username );
					if( strtolower( $user['email'] ) == strtolower( $email ) )
						return $this->messenger->noteError( $this->words['user']['msg']['error5'], $email );
				}
				if( $password == $confirm )
				{
					$data		= array(
						"username"	=> $username,
						"email"		=> $email,
						"password"	=> md5( $password ),
						"notify"		=> $request->get( 'add_notify' ),
						);
					$user	= new User();
					$user_id	= $user->addData( $data );

					$data		= array(
						"user_id"		=> $user_id,
						"role_id"		=> $role_id,
						);
					$role	= new UserRole();
					$role->addData( $data );
					$request->remove( 'add_username' );
					$request->remove( 'add_email' );
					$request->remove( 'add_password' );
					$request->remove( 'add_confirm' );
					$request->remove( 'add_notify' );
					$this->messenger->noteSuccess( $this->words['user']['msg']['success1'], $username );
				}
				else
					return $this->messenger->noteError( $this->words['user']['msg']['error7'] );
			}
		}
	}

	/**
	 *	Edits an existing User.
	 *	@access		private
	 *	@return		void
	 */
	private function editUser()
	{
		$request	= $this->ref->get( 'request' );
		$lan		= $this->ref->get( 'language' );
		if( $this->validateForm( 'user', 'editUser', 'user', 'edit' ) )
		{
			$user_id		= $request->get( 'user_id' );
			$username	= $request->get( 'edit_username' );
			$email		= $request->get( 'edit_email' );

			$user		= new User();
			$user->focusPrimary( $user_id );
			$userdata	= $user->getData( false, true );
			$users		= $user->getAllData();
			$users		= $users[$user->getTableName()];
			foreach( $users as $entry )
			{
				if( $entry['user_id'] != $user_id && strtolower( $entry['username'] ) == strtolower( $username ) )
					return $this->messenger->noteError( $this->words['user']['msg']['error3'], $username );
				if( $entry['user_id'] != $user_id && strtolower( $entry['email'] ) == strtolower( $email ) )
					return $this->messenger->noteError( $this->words['user']['msg']['error5'], $email );
			}
			$data		= array(
				"username"	=> $username,
				"email"		=> $email,
				"notify"		=> (int)$request->get( 'edit_notify' ),
				);
			$user->modifyData( $data );
			if( $password = $request->get( 'edit_password' ) )
			{
				$confirm	= $request->get( 'edit_confirm' );
				if( $password == $confirm )
				{
//					$data	= array( "password"	=> md5( $password ) );
					$user->modifyData( array( "password"	=> md5( $password ) ) );
					$this->messenger->noteSuccess( $this->words['user']['msg']['success2'], $userdata['username'] );
				}
				else
					return $this->messenger->noteError( $this->words['user']['msg']['error7'] );
			}
			else
			{
				$user->modifyData( $data );
				$this->messenger->noteSuccess( $this->words['user']['msg']['success2'], $userdata['username'] );
			}
			$request->remove( 'user_id' );
		}
	}

	/**
	 *	Removes an existing User.
	 *	@access		private
	 *	@return		void
	 */
	private function removeUser()
	{
		$request	= $this->ref->get( 'request' );
		if( $user_id = $request->get( 'user_id' ) )
		{
			$role	= new UserRole();
			$role->focusForeign( "user_id", $user_id );
			$roles	= $role->getData( false );
			$roles_has	= array();
			foreach( $roles as $role )
				$roles_has[]	= $role['role_id'];
			$request->set( "roles_has", $roles_has );
			$this->removeRole();

			$user	= new User( $user_id );
			$data	= $user->getData( false, true );
			$user->deleteData();
			$request->remove( 'user_id' );
			$this->messenger->noteSuccess( $this->words['user']['msg']['success3'], $data['username'] );
		}
		else
			$this->messenger->noteError( $this->words['user']['msg']['error1'] );
	}

	/**
	 *	Adds a Role to a User.
	 *	@access		private
	 *	@return		void
	 */
	private function addRole()
	{
		$request	= $this->ref->get( 'request' );
		if( $user_id = $request->get( 'user_id' ) )
		{
			if( is_array( $roles = $request->get( 'roles' ) ) && count( $roles ) )
			{
				$user	= new User();
				$user->focusPrimary( $user_id );
				$user	= $user->getData( false, true );

				$role	= new Role();
				$userrole	= new UserRole();
				foreach( $roles as $role_id )
				{
					$role->focusPrimary( $role_id );
					$roledata	= $role->getData( false, true );
					$userrole->addData( array( "user_id" => $user_id, "role_id" => $role_id ) );
					$this->messenger->noteSuccess( $this->words['user']['msg']['success4'], $roledata['rolename'], $user['username'] );
				}
			}
			else
				$this->messenger->noteError ( $this->words['user']['msg']['error8'] );
		}
		else
			$this->messenger->noteError( $this->words['user']['msg']['error1'] );
	}

	/**
	 *	Removes a Role from a User.
	 *	@access		private
	 *	@return		void
	 */
	private function removeRole()
	{
		$request	= $this->ref->get( 'request' );
		if( $user_id = $request->get( 'user_id' ) )
		{
			if( is_array( $roles = $request->get( 'roles_has' ) ) && count( $roles ) )
			{
				$user	= new User();
				$user->focusPrimary( $user_id );
				$user	= $user->getData( false, true );

				$role	= new Role();
				$userrole	= new UserRole();
				foreach( $roles as $role_id )
				{
					$role->focusPrimary( $role_id );
					$role	= $role->getData( false, true );
					$userrole->focusForeign( "user_id", $user_id );
					$userrole->focusForeign( "role_id", $role_id );
					$userrole->deleteData();
					$this->messenger->noteSuccess( $this->words['user']['msg']['success5'], $role['rolename'], $user['username'] );
				}
			}
			else
				$this->messenger->noteError ( $this->words['user']['msg']['error8'] );
		}
		else
			$this->messenger->noteError( $this->words['user']['msg']['error1'] );
	}

	/**
	 *	Sets current Filters in Session.
	 *	@access		private
	 *	@return		void
	 */
	private function filterUser()
	{
		$config		= $this->ref->get( 'config' );
		$session	= $this->ref->get( 'session' );
		$request	= $this->ref->get( 'request' );

		if( $request->get( 'filterUsers' ) )
		{
			$session->set( 'user_username',		$request->get( 'filter_username' ) );
			$session->set( 'user_email',		$request->get( 'filter_email' ) );
			$session->set( 'user_order',		$request->get( 'filter_order' ) );
			$session->set( 'user_direction',	$request->get( 'filter_direction' ) );
			$session->set( 'user_limit',		$request->get( 'filter_limit' ) );
			$session->set( 'user_offset',		0 );
		}
		else
		{
			if( NULL !== ( $offset = $request->get( 'offset' ) ) )
				$session->set( 'user_offset', $offset );
			if( !$session->get( 'user_limit' ) )
				$session->set( 'user_limit', $config['layout']['list_limit'] );
		}

		if( !$session->get( 'user_limit' ) )
			$session->set( 'user_limit', $config['layout']['list_limit'] );
		if( $session->get( 'user_offset' ) < 0 )
			$session->set( 'user_offset', 0 );
	}
}
?>